<?php
namespace app\admin\controller;

use app\common\model\Friends as FriendsModel;
use app\common\model\Member as MemberModel;
use app\common\model\Mzongpai as MzongpaiModel;
use app\common\model\MemberQuan as MemberQuanModel;
use app\common\model\Ilike as IlikeModel;
use app\common\model\Mjifen as MjifenModel;
use app\common\model\BuddhistService as BuddhistServiceModel;
use app\common\model\Gatherreg as GatherregModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;
use app\common\model\NewsComment as NewsCommentModel;
use app\common\model\Recharge as RechargeModel;

/**
 * 好友控制器
 * Class Friends
 * @package app\index\controller
 */
class Friends extends Base
{
    /**
     * [getIndex  查看]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $this->getPageAndSize($param);
        $model = new FriendsModel;
        $mmodel = new MemberModel;
        $zmodel = new MzongpaiModel;
        $qmodel = new MemberQuanModel();
        $imodel = new IlikeModel();
        $jmodel = new MjifenModel();
        $bmodel = new BuddhistServiceModel();
        $gmodel = new GatherregModel();
        $cmodel = new CompanyGongdengOrderModel;
        $remodel = new RechargeModel;
        $ncmodel = new NewsCommentModel;
        $map = [];
        try{
            $map['memid|friend_id'] = $param['id'];
            $map['states'] = 1;
            $field = 'memid,friend_id';
            $count = $model->getCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getByConditon($map,$this->from, $this->size,$field);
            $i = 0;
            foreach($lists as $k=>$v){
                if($v['memid'] == $param['id']){
                    $friend = $mmodel->findData($v['friend_id'],'id','id,tel,username,img,jifen,addtime,city,bathday,zid,identity,qrcode,token');
                }else{
                    $friend = $mmodel->findData($v['memid'],'id','id,tel,username,img,jifen,addtime,city,bathday,zid,identity,qrcode,token');
                }

                if(!empty($friend)){
                    if(!empty($friend['zid'])){
                        $zongpai = $zmodel->findData($friend['zid'],'id');
                        $friend['zongpai'] = $zongpai['name'];
                    }else{
                        $friend['zongpai'] = '无';
                    }
                    $friend['friend_num'] = $model->getAllCount(['memid|friend_id'=>$friend['id'],'states'=>1]);
                    $quan = $qmodel->getNum(['member_id'=>$friend['id']]);
                    $friend['quan'] = $quan;

                    $friend['guanzhu'] = $imodel->getCount(['i.memid'=>$friend['id'],'i.status'=>1]);
                    if(!empty($friend['qrcode'])){
                        $friend['qrcode'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$friend['qrcode'];
                    }else{
                        $qrcode = Qrcode($friend['token'],'https://wx.fushan8.com/#/invitePage?id='.$friend['id']);
                        $mmodel->editData(['id'=>$friend['id'],'qrcode'=>$qrcode]);
                        $friend['qrcode'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$qrcode;
                    }
                    $friend['invite_url'] = 'https://wx.fushan8.com/#/invitePage?id='.$friend['id'];
                    $friend['addtime'] = date('Y-m-d H:i:s',$friend['addtime']);
                    $friend['invite_num'] = $jmodel->getAllCount(['memid'=>$friend['id']]);
                    $friend['buddhist_num'] = $bmodel->getAllCount(['memid'=>$friend['id']]);
                    $friend['baoming_num'] = $gmodel->getAllCount(['memid'=>$friend['id']]);
                    $cnum = $cmodel->getSum(['mid'=>$friend['id'],'status'=>1],'total_price');
                    $renum = $remodel->getSum(['memid'=>$friend['id'],'states'=>1],'amt');
                    $friend['shankuan'] = $cnum+$renum;
                    $friend['pinlun'] = $ncmodel->getAllCount(['mid'=>$friend['id']]);
                    $http = substr($friend['img'],0,4);
                    if($http != 'http'){
                        $friend['img'] = config('domain_name').$friend['img'];
                    }
                    $lists[$i] = $friend;
                    $i++;
                }
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
}